<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>医院设置列表</span>
      </div>

      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="医院名称" prop="hosname">
          <el-input
            v-model="searchObj.hosname"
            placeholder="医院名称"
            size="small"
            clearable
          />
        </el-form-item>
        <el-form-item label="医院编号" prop="hoscode">
          <el-input
            v-model="searchObj.hoscode"
            placeholder="医院编号"
            size="small"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            >导出</el-button
          >
        </el-col>
      </el-row>

      <el-table
        v-loading="loading"
        :data="resList"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="55">
          <template slot-scope="scope">
            {{ (currentPage - 1) * currentSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="hosname" align="center" label="医院名称" />
        <el-table-column
          prop="hoscode"
          align="center"
          width="80"
          label="医院编号"
        />
        <el-table-column align="center" prop="apiUrl" label="API基础路径" />
        <el-table-column
          prop="contactsName"
          align="center"
          label="联系人姓名"
        />
        <el-table-column
          align="center"
          prop="contactsPhone"
          label="联系人手机"
        />
        <el-table-column align="center" label="状态" width="80">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === 1 ? 'primary' : 'danger'"
              disable-transitions
            >
              {{ scope.row.status === 1 ? "启用" : "禁用" }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        >
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              v-if="scope.row.status == 1"
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="lockHostSet(scope.row.id, 0)"
              >锁定</el-button
            >
            <el-button
              v-if="scope.row.status == 0"
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="lockHostSet(scope.row.id, 1)"
              >解锁</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-show="total > 0"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 25, 30, 50]"
        :page-size="currentSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </el-card>

    <!-- 添加/修改 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="医院名称" prop="hosname">
          <el-input v-model="form.hosname" placeholder="请输入医院名称" />
        </el-form-item>
        <el-form-item label="医院编号" prop="hoscode">
          <el-input v-model="form.hoscode" placeholder="请输入医院编号" />
        </el-form-item>
        <el-form-item label="API基础路径" prop="apiUrl">
          <el-input v-model="form.apiUrl" placeholder="请输入API基础路径" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactsName">
          <el-input
            v-model="form.contactsName"
            placeholder="请输入联系人姓名"
          />
        </el-form-item>
        <el-form-item label="联系人手机" prop="contactsPhone">
          <el-input
            v-model="form.contactsPhone"
            placeholder="请输入联系人手机"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getHospList,
  getHospById,
  saveHospSet,
  lockHospSet,
  removeHospSet
} from "@/api/hospset";
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      currentPage: 1,
      currentSize: 10,
      searchObj: {
        hosname: "",
        hoscode: ""
      },
      resList: [],
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hosname: [
          { required: true, message: "医院名称不能为空", trigger: "blur" }
        ],
        hoscode: [
          { required: true, message: "医院编号不能为空", trigger: "blur" }
        ],
        apiUrl: [
          { required: true, message: "API基础路径不能为空", trigger: "blur" }
        ],
        contactsName: [
          { required: true, message: "联系人姓名不能为空", trigger: "blur" }
        ],
        contactsPhone: [
          { required: true, message: "联系人手机不能为空", trigger: "blur" }
        ]
      }
    };
  },
  //在页面渲染之前执行
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getHospList(this.currentPage, this.currentSize, this.searchObj)
        .then(response => {
          this.resList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        })
        .catch(error => {
          console.log(error);
        });
    },
    handleSizeChange(val) {
      this.currentSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    handleQuery() {
      this.currentPage = 1;
      this.getList();
    },
    resetQuery() {
      this.currentPage = 1;
      this.currentSize = 10;
      this.searchObj = {
        hosname: "",
        hoscode: ""
      };
      this.getList();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    lockHostSet(id, status) {
      lockHospSet(id, status).then(response => {
        this.$message({ type: "success", message: "操作成功!" });
        this.getList();
      });
    },
    /** 表单重置 */
    reset() {
      this.form = {
        hosname: undefined,
        hoscode: undefined,
        apiUrl: undefined,
        contactsName: undefined,
        contactsPhone: undefined
      };
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "医院设置添加";
    },
    handleUpdate(row) {
      this.reset();
      const uid = row.id || this.ids;
      getHospById(uid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "医院设置更新";
      });
    },
    handleDelete(row) {
      const delId = row.id;
      this.$confirm(
        '是否确认删除医院设置信息医院名称为" ' + row.hosname + ' "的数据项？'
      )
        .then(() => {
          // removeHospSet(delId).then(response => {
          //   this.$message({ type: "success", message: "删除成功!" });
          //   this.getList();
          // });
          this.getList();
        })
        .catch(() => {});
    },
    handleExport() {},
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            console.log("update");
          } else {
            console.log("add");
          }
        }
      });
    }
  }
};
</script>
